package com.ebeitech.doorapp.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.ebeitech.doorapp.R;
import com.ebeitech.doorapp.constants.ActionConstants;
import com.ebeitech.library.util.view.StatusBarCompat;

/**
 * Created by alice on 2017/10/24.
 */
public class BaseActivity extends FragmentActivity {

	private boolean isOnPause;
	private boolean isFitStatusBar = true;

	private Context mContext;

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#startActivity(android.content.Intent)
	 */
	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		super.startActivity(intent);
		overridePendingTransition(R.anim.ebei_in_from_right, R.anim.ebei_out_to_left);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#startActivityForResult(android.content.Intent,
	 * int, android.os.Bundle)
	 */
	@Override
	public void startActivityForResult(Intent intent, int requestCode,
									   Bundle options) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode, options);
		overridePendingTransition(R.anim.ebei_in_from_right, R.anim.ebei_out_to_left);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		isFitStatusBar = StatusBarCompat.setStatusBar(this, arg0);
		if (isFitStatusBar) {
			StatusBarCompat.compat(this);
		}
		mContext = this;
		registerBroadCast();
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (isFitStatusBar) {
			StatusBarCompat.fitsSystemWindows(this);
		}
	}

	/*
		 * (non-Javadoc)
		 *
		 * @see android.app.Activity#finish()
		 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.ebei_in_from_left, R.anim.ebei_out_to_right);
	}

	private void registerBroadCast() {
		IntentFilter myIntentFilter = new IntentFilter();
		myIntentFilter.addAction(ActionConstants.ACTION_EXIT_APP);
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (ActionConstants.ACTION_EXIT_APP.equals(action)) {
				finish();
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isOnPause = false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		isOnPause = true;
	}

	public boolean isOnPause() {
		return isOnPause;
	}

	public void exit() {

	}

}
